package com.e_manager.soft.emanager.util;

import com.e_manager.soft.emanager.token.TokenModel;

/**
 * @author 徐玲玉
 * 2018/3/15 0015
 */
public class StringUtil {

    private static StringUtil instance;
    /**
     * 单例返回对象
     *
     * @return StringUtil
     */
    public static StringUtil getInstance() {
        if (instance == null) {
            instance = new StringUtil();
        }
        return instance;
    }

    /**
     * 获取authentication中的userID和token
     * @param authentication 验证信息，格式为 id_token
     * @return TokenModel
     * @see com.e_manager.soft.emanager.token.TokenModel
     */
    public TokenModel getToken(String authentication) {
        if (authentication == null || authentication.length() == 0) {
            return null;
        }
        String[] param = authentication.split("_");
        if (param.length != 2) {
            return null;
        }
        //authentication解析成id和token
        String userId = null;
        try {
            userId = param[0];
        } catch (Exception e) {
            return null;
        }
        String token = param[1];
        return new TokenModel(userId, token);
    }
}
